#include "system.h"

void set_lobby(struct lobby *clients) {

    int i; 
    for(i = 0; i < MAXPLAYERS; i++) {

        clients[i].player_id = -1;
        clients[i].player_name[0] = 0;
        clients[i].player_ip[0] = 0;
        clients[i].player_port = -1;
        clients[i].player_socket = -1;
        clients[i].player_thread = -1;
        clients[i].challenge_player_id = -1;
        clients[i].created_game = -1;
        clients[i].character = -1;
    }
}

int free_index(struct lobby *clients) {

    int i;
    for(i = 0; i < MAXPLAYERS; i++) {

        if(clients[i].player_id == -1) {

            return i;
        }
    }
    return -1;
}

void make_demon(void) {

  pid_t pid, sid;

  if( getppid() == 1 ) return;

  pid = fork();
  if( pid < 0 ) {

    exit(1);
  }

  if( pid > 0 ) {
    
    exit(0);
  }

  umask(0);

  sid = setsid();
  if (sid < 0) {
    exit(EXIT_FAILURE);
  }

  if ((chdir("/home/me")) < 0) {
    exit(EXIT_FAILURE);
  }

  /* Redirect standard files to /dev/null */
  freopen( "/dev/null", "r", stdin);
  freopen( "/dev/null", "w", stdout);
  freopen( "/dev/null", "w", stderr);
}
